/*
 *作者：姚俊
 *功能：发送邮件工具
 *日期：2015-05-24
 */
package admin.email;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmail {
	private static transient Session session=null;
	private static String fromUser="459828686@qq.com";
	private static String fromPass="taoxiaoqing888";

    public static void send_email(String toUser,String subject1,String content1) throws IOException, AddressException, MessagingException{
    	//发送邮件人的邮箱地址
        String to = toUser;
        //发送邮件的主题
        String subject = subject1;
        //发送邮件的内容
        String content = content1;
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com");//邮箱服务器的包括smtp协议和域名
        properties.put("mail.smtp.port", "25");//邮箱服务器的端口号
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator(fromUser, fromPass);
        session= Session.getInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(session);
        mailMessage.setFrom(new InternetAddress(fromUser));
        // Message.RecipientType.TO属性表示接收者的类型为TO
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}


